{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 导入需要的requests模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0. 人脸API第一步"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390249,e302755e-5378-4801-bb81-73d08c348241',\n",
       " 'time_used': 186,\n",
       " 'faces': [{'face_token': 'c22f8460b582b4b3c0b86f6f69822c8c',\n",
       "   'face_rectangle': {'top': 258, 'left': 128, 'width': 283, 'height': 283}}],\n",
       " 'image_id': 'U5CpILjjZXWZQH86bdF31Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 2. 输入我们需要API网站注册的API_Key\n",
    "API_Key = \"gga5PMju7A9CwccEhoXt2BZLLVWY7uwO\"\n",
    "API_secret = \"w8Ea0nZGOdpqqlkkjdXlFg_RpjW9ezxs\"\n",
    "# 3. 准备API测试接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://img0.baidu.com/it/u=2164690002,4129730642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750\"\n",
    "}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390249,e302755e-5378-4801-bb81-73d08c348241',\n",
       " 'time_used': 186,\n",
       " 'faces': [{'face_token': 'c22f8460b582b4b3c0b86f6f69822c8c',\n",
       "   'face_rectangle': {'top': 258, 'left': 128, 'width': 283, 'height': 283}}],\n",
       " 'image_id': 'U5CpILjjZXWZQH86bdF31Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gga5PMju7A9CwccEhoXt2BZLLVWY7uwO'\n",
    "API_sercret = 'w8Ea0nZGOdpqqlkkjdXlFg_RpjW9ezxs'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens': '8e0bd1469f602ceb22b4615614171dbe',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 299,\n",
       " 'request_id': '1648390255,c421954e-c245-4b43-912b-4ad6efa0fe81',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.076,\n",
       "     'neutral': 60.198,\n",
       "     'disgust': 25.552,\n",
       "     'anger': 13.153,\n",
       "     'surprise': 0.076,\n",
       "     'fear': 0.283,\n",
       "     'happiness': 0.662},\n",
       "    'beauty': {'female_score': 68.123, 'male_score': 67.195},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 24},\n",
       "    'mouthstatus': {'close': 100.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.239,\n",
       "     'stain': 58.016,\n",
       "     'acne': 52.408,\n",
       "     'health': 24.787},\n",
       "    'headpose': {'yaw_angle': -6.159368,\n",
       "     'pitch_angle': 11.474036,\n",
       "     'roll_angle': 2.258761},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.362},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.362},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.362}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.616},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.004,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.999,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 83.985},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.52,\n",
       "      'vector_z_component': 0.945,\n",
       "      'vector_x_component': 0.028,\n",
       "      'vector_y_component': -0.327,\n",
       "      'position_y_coordinate': 0.372},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.479,\n",
       "      'vector_z_component': 0.922,\n",
       "      'vector_x_component': -0.104,\n",
       "      'vector_y_component': -0.373,\n",
       "      'position_y_coordinate': 0.4}}},\n",
       "   'face_rectangle': {'width': 283, 'top': 258, 'left': 128, 'height': 283},\n",
       "   'face_token': '8e0bd1469f602ceb22b4615614171dbe'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390264,3a91dbf2-4d33-4cfb-8840-78ce2baec2bc',\n",
       " 'time_used': 286,\n",
       " 'faces': [{'face_token': '5b5e7e8c350db4e35b7bde0fd2b1d1de',\n",
       "   'face_rectangle': {'top': 267, 'left': 103, 'width': 466, 'height': 466}}],\n",
       " 'image_id': 'WaepaM0OVTSBhz9A6OqaFw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 2. 输入我们需要API网站注册的API_Key\n",
    "API_Key = \"gga5PMju7A9CwccEhoXt2BZLLVWY7uwO\"\n",
    "API_secret = \"w8Ea0nZGOdpqqlkkjdXlFg_RpjW9ezxs\"\n",
    "# 3. 准备API测试接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fdcfb3ddcdfd473a8c2b539b142f55b06f5cfa6411743d-7bNmMD_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907216&t=2fc784fe680f6b57a89970d70d094132\"\n",
    "}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390264,3a91dbf2-4d33-4cfb-8840-78ce2baec2bc',\n",
       " 'time_used': 286,\n",
       " 'faces': [{'face_token': '5b5e7e8c350db4e35b7bde0fd2b1d1de',\n",
       "   'face_rectangle': {'top': 267, 'left': 103, 'width': 466, 'height': 466}}],\n",
       " 'image_id': 'WaepaM0OVTSBhz9A6OqaFw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390350,8fa95346-3b65-4eba-a6c1-33b9af744f81',\n",
       " 'time_used': 282,\n",
       " 'confidence': 47.26,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 2. 输入我们需要API网站注册的API_Key\n",
    "API_Key = \"gga5PMju7A9CwccEhoXt2BZLLVWY7uwO\"\n",
    "API_secret = \"w8Ea0nZGOdpqqlkkjdXlFg_RpjW9ezxs\"\n",
    "# 3. 准备API测试接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    'face_token1': '8e0bd1469f602ceb22b4615614171dbe',\n",
    "    'face_token2': '5b5e7e8c350db4e35b7bde0fd2b1d1de',\n",
    "}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390350,8fa95346-3b65-4eba-a6c1-33b9af744f81',\n",
       " 'time_used': 282,\n",
       " 'confidence': 47.26,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.封装应用函数（内部使用的接口）\n",
    "## 1. 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'\n",
    "API_sercret = 'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648372531,e415809c-bde8-4413-9528-f094a1b572fa',\n",
       " 'time_used': 667,\n",
       " 'faces': [{'face_token': '32c2dfa5414f6cc3d461c681ac6b4602',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648372539,f7aa6ed8-717a-4d06-943c-f2bd9365144d',\n",
       " 'time_used': 638,\n",
       " 'faces': [{'face_token': '54215bfe12a65e45912d657fac519b21',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
